package com.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author: chenyuan.wang
 * @Description: TODO
 * @Date: 2019/9/3 13:59
 * @Version 1.0
 */
public class ThreadTest {


    public static void main(String[] args) {
       /* List<Integer> list = new ArrayList<Integer>();
        list.add(19);
        list.add(18);
        list.add(17);

        ForkJoinPool forkJoinPool = new ForkJoinPool(2);
        CompletableFuture<List<String>> futureResult = CompletableFuture.supplyAsync(
                () -> list.stream().parallel().map(item -> String.valueOf(item)).collect(Collectors.toList()), forkJoinPool);
        futureResult.whenComplete((r, e) -> System.out.println(r));

        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/


        ForkJoinPool forkJoinPool = new ForkJoinPool(5);
        try {
            for(int i=0;i<10;i++) {
                long count = forkJoinPool.submit(() -> {
                    System.out.println("thread:"+Thread.currentThread().getId());
                    return IntStream.range(0, 10000).parallel().filter(item->item>999).count();
                }).get();
                System.out.println(count);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println(Runtime.getRuntime().availableProcessors() + "-" + ForkJoinPool.getCommonPoolParallelism());
    }

}
